package com.xh.rufongwallpaper.controller;

import com.xh.rufongwallpaper.common.result.Result;
import com.xh.rufongwallpaper.common.util.VOConverter;
import com.xh.rufongwallpaper.entity.Category;
import com.xh.rufongwallpaper.service.CategoryService;
import com.xh.rufongwallpaper.service.WallpaperService;
import com.xh.rufongwallpaper.vo.CategoryVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 分类控制器
 *
 * @author XH
 */
@RestController
@RequestMapping("/categories")
@RequiredArgsConstructor
@Tag(name = "分类API", description = "壁纸分类相关接口")
public class CategoryController {

    private final CategoryService categoryService;
    private final WallpaperService wallpaperService;

    /**
     * 获取所有分类
     */
    @GetMapping
    @Operation(summary = "获取所有分类", description = "获取所有壁纸分类")
    public Result<List<CategoryVO>> getAllCategories() {
        List<Category> categories = categoryService.getAllCategories();
        
        // 获取每个分类下的壁纸数量
        List<Integer> wallpaperCounts = new ArrayList<>();
        for (Category category : categories) {
            int count = wallpaperService.countWallpapersByCategory(category.getId());
            wallpaperCounts.add(count);
        }
        
        // 转换为前端所需的VO对象
        List<CategoryVO> categoryVOs = VOConverter.convertToCategoryVOList(categories, wallpaperCounts);
        
        return Result.success(categoryVOs);
    }

    /**
     * 根据ID获取分类
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取分类", description = "根据ID获取分类详情")
    public Result<CategoryVO> getCategoryById(
            @Parameter(description = "分类ID") @PathVariable Long id) {
        
        return categoryService.getCategoryById(id)
                .map(category -> {
                    // 获取该分类下的壁纸数量
                    int count = wallpaperService.countWallpapersByCategory(category.getId());
                    
                    // 转换为前端所需的VO对象
                    CategoryVO categoryVO = VOConverter.convertToCategoryVO(category, count);
                    
                    return Result.success(categoryVO);
                })
                .orElse(Result.failure("分类不存在"));
    }

    /**
     * 根据编码获取分类
     *
     * @param code 分类编码
     * @return 分类信息
     */
    @GetMapping("/code/{code}")
    @Operation(summary = "根据编码获取分类", description = "根据编码获取分类详情")
    public Result<Category> getCategoryByCode(
            @Parameter(description = "分类编码") @PathVariable String code) {
        return categoryService.getCategoryByCode(code)
                .map(Result::success)
                .orElse(Result.failure("分类不存在"));
    }
} 